package zhangyi.tdd.ecommerce.domain;

import com.google.common.base.Strings;
import zhangyi.tdd.ecommerce.domain.exception.InvalidAddressException;
import zhangyi.tdd.ecommerce.domain.exception.InvalidOrderException;

public class Order {
    private String customerId;
    private Address shippingAddress;

    public Order(String customerId, Address shippingAddress) {
        this.customerId = customerId;
        this.shippingAddress = shippingAddress;
    }

    public void validate() {
        validateCustomerId();
        validateShippingAddress();
    }

    private void validateCustomerId() {
        if (Strings.isNullOrEmpty(customerId)) {
            throw new InvalidOrderException("No customer id");
        }
    }

    private void validateShippingAddress() {
        if (shippingAddress == null) {
            throw new InvalidOrderException("No Shipping Address");
        }
        try {
            shippingAddress.validate();
        } catch (InvalidAddressException ex) {
            throw new InvalidOrderException("Invalid Shipping Address", ex);
        }
    }
}
